package com.ruoyi.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.stu.mapper.StuStudentUnitMapper;
import com.ruoyi.stu.mapper.StuStudentWordMapper;
import com.ruoyi.system.domain.SysWordLearningProgress;
import com.ruoyi.system.mapper.SysWordLearningProgressMapper;
import com.ruoyi.system.service.ISysWordLearningProgressService;

/**
 * 单词学习进度服务实现
 */
@Service
public class SysWordLearningProgressServiceImpl implements ISysWordLearningProgressService
{
    @Autowired
    private StuStudentUnitMapper stuStudentUnitMapper;
    @Autowired
    private StuStudentWordMapper stuStudentWordMapper;
    @Autowired
    private SysWordLearningProgressMapper wordLearningProgressMapper;

    /**
     * 保存单词学习进度
     */
    @Override
    public int saveWordLearningProgress(SysWordLearningProgress progress)
    {
        int i =  wordLearningProgressMapper.insertWordLearningProgress(progress);
        
        Long studentId = progress.getStudentId();
        Long courseId = progress.getCourseId();
        Long unitId = progress.getUnitId();
        Integer numStudyed = wordLearningProgressMapper.sumStudyed(studentId, courseId, unitId, null, null);
        Integer numWordOfUnit = stuStudentWordMapper.count(studentId, unitId, null);
        if (numStudyed >= numWordOfUnit) {
        	stuStudentUnitMapper.updateStateByStudentIdAndUnitId(studentId, unitId, "state_study", 1);
		}
        return i;
    }

    /**
     * 查询用户在指定课程和单元的学习进度
     */
    @Override
    public SysWordLearningProgress list(Long studentId, Long courseId, Long unitId)
    {
        return wordLearningProgressMapper.list(studentId, courseId, unitId, null);
    }
}